#!/bin/bash
###############################################################################
#
#  EGSnrc script to build the g-code documentation.
#  Copyright (C) 2020 National Research Council Canada
#
#  This file is part of EGSnrc.
#
#  EGSnrc is free software: you can redistribute it and/or modify it under
#  the terms of the GNU Affero General Public License as published by the
#  Free Software Foundation, either version 3 of the License, or (at your
#  option) any later version.
#
#  EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
#  more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
#
#  Author:          Ernesto Mainegra-Hing, 2020
#
#  Contributors:
#
###############################################################################
#
# Documentation built using Doxygen's ability to parse comments from the
# Fortran source from $EGS_HOME/g. Make sure it is the up-to-date version!
#
###############################################################################

my_machine=`cat $EGS_CONFIG | grep "my_machine =" | sed 's/my_machine = //'`
docdir=$PWD
eh=$EGS_HOME
hh=$HEN_HOUSE
update_eq=no
latex_build=yes
#
# Parse command line for arguments
#
while test $# -gt 0; do
    case "$1" in
      dir=*) docdir=`echo $1 | sed 's/dir=//'` ;;
       eh=*) eh=`echo $1 | sed 's/eh=//'` ;;
       eq=*) update_eq=`echo $1 | sed 's/eq=//'` ;;
    latex=*) latex_build=`echo $1 | sed 's/eq=//'` ;;
   config=*) my_machine=`echo $1 | sed 's/config=//'` ;;
    esac
    shift
done

echo PWD: $PWD
echo docdir: $docdir
echo EGS_HOME: $eh
echo config: $my_machine

######################################################
# What if no EGSnrc? Use the provided Fortran source!
######################################################
if test "x$my_machine" = x; then
 my_machine=template
fi

if test -d "$eh"; then
 cd $eh/g
 make fortran
 echo 'C> @endcond' >> g_${my_machine}.f
 # Doxygen processes all *.f files, rename template temporarily
 mv $docdir/g_template.f $docdir/g_template.f.OK
 rm -f $docdir/*.f
 cp g_${my_machine}.f $docdir/g.f
else
 cp g_${my_machine}.f g.f
 mv g_template.g_template.f.OK
fi

if test "$docdir" != $PWD; then
 cd $docdir
fi

# Needed if equations modified
if [[ "$update_eq" == yes ]];then
   rm -f html/*form*.*
fi
# Run doxygen
doxygen
# Restore template
mv $docdir/g_template.f.OK $docdir/g_template.f

###########################################
# Process Latex refman
##########################################
if [[ "$latex_build" == yes ]];then
   filename=latex/refman.tex
   # Update authors on Latex refman
   sed -e "s/large Generated by/large E. Mainegra-Hing, D.W.O. Rogers, R.W. Townson, B.R.B. Walters, F. Tessier and I. Kawrakow}\\\\\\\ \n\\\vspace*{1cm}\n{\\\large Generated on \\\today~ with /g" -i $filename
   # Switch to Inconsolata
   sed -e "s/courier/inconsolata/g" -i $filename
 cd latex;make;mv refman.pdf ../../pirs3100-g-refman.pdf;cd ..
fi

exit 0

